# run-consumers.ps1
param(
    [int]$Count = 5,
    [int]$BasePort = 6061
)

$pidFile = "consumer-pids.txt"

if (Test-Path $pidFile) {
    Remove-Item $pidFile
}

Write-Host "🚀 Starting $Count consumer instances (ports $BasePort - $($BasePort + $Count - 1))..." -ForegroundColor Green

for ($i = 1; $i -le $Count; $i++) {
    $instanceId = "pod-$i"
    $pprofPort = $BasePort + $i - 1

    Write-Host "  → Launching $instanceId (PPROF_PORT=$pprofPort)" -ForegroundColor Cyan

    $process = Start-Process -FilePath "go" `
        -ArgumentList "run", "main.go" `
        -Environment @{"INSTANCE_ID" = $instanceId; "PPROF_PORT" = "$pprofPort"} `
        -PassThru `
        -NoNewWindow

    Add-Content -Path $pidFile -Value $process.Id
}

Write-Host "`n✅ All consumers started!" -ForegroundColor Green
Write-Host "📊 To profile pod-1: go tool pprof http://localhost:$BasePort/debug/pprof/heap" -ForegroundColor Yellow
Write-Host "🛑 To stop all, run: .\stop-consumers.ps1" -ForegroundColor Red